package main

import (
	"fmt"
)

const N = 10

// 每个塔的数据
var arr [3][N]int = [3][N]int{{0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}}

func main() {
	// fmt.Println("a")

	dataInit(5)

	HanIO(5, "A", "B", "C")
}

// 初始化
func dataInit(n int) {
	for i := 0; i < n; i++ {
		arr[0][N-1-i] = n - i
	}
}

// 显示数据
func show() {
	fmt.Printf("%5s%5s%5s\n", "A", "B", "C")
	for i := 0; i < N; i++ {
		for j := 0; j < 3; j++ {
			fmt.Printf("%5d", arr[j][i])
		}
		fmt.Println()
	}
}

func HanIO(n int, A, B, C string) {
	if n < 1 {
		return
	}
	if n == 1 {
		fmt.Printf("%s->%s\n", A, C)
		move(A, C)
		show()
	} else {

		HanIO(n-1, A, C, B)
		fmt.Printf("%s->%s\n", A, C)
		move(A, C)
		show()
		HanIO(n-1, B, A, C)
	}

}

//
func move(X, Y string) {
	var m int = int(X[0]) - 65
	var n int = int(Y[0]) - 65

	var imove = -1 //保存第一个不等一0的索引

	for i := 0; i < N; i++ {
		if arr[m][i] != 0 {
			imove = i
			break
		}
	}
	fmt.Println(m, "===", n)
	fmt.Println("imove=", arr[m][imove])

	var jmove int
	if arr[n][N-1] == 0 {
		jmove = N - 1
	} else {
		jmove = N
		for i := 0; i < N; i++ {
			if arr[n][i] != 0 {
				jmove = i
				break
			}
		}
		fmt.Println("jmove", jmove)
		jmove -= 1
	}
	fmt.Println(X, "==", imove, "\n", "n=", Y, jmove)
	arr[m][imove], arr[n][jmove] = arr[n][jmove], arr[m][imove]
}
